package com.plugin.sms;

import com.core.shared.DateTimeUtils;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * Created with IntelliJ IDEA.
 * User: lxf
 * Date: 2016/10/18
 * Time: 16:45
 * To change this template use Editor | File and Code Templates.
 */
public class SendSmsHandle {

    private static Map<String, String> smsCacheMap = new HashMap<>();
    private static final String sms_account = "101100-WEB-HUAX-402801";
    private static final String sms_password = "RXYDMYPC";

    private SendSmsHandle() {
    }

    /**
     * 华兴软通短信平台
     *
     * @param message
     * @param phoneNum
     * @return
     */
    public static String sendValidateNum(String message, String phoneNum) {
        String strReg = sms_account;   //注册号
        String strPwd = sms_password;  //密码
        String strSourceAdd = "";         //子通道号，可为空（预留参数一般为空）
        String strPhone = phoneNum;         //手机号码，多个手机号用半角逗号分开，最多1000个
        String validateNum = createValidateNum();   //生成4位验证码
        String content = message.replaceAll("\\{0\\}", validateNum);
        String strContent = SmsHttpSend.paraTo16(content);
        String strSmsUrl = "http://www.stongnet.com/sdkhttp/sendsms.aspx";
        String strSmsParam = "reg=" + strReg + "&pwd=" + strPwd + "&sourceadd=" + strSourceAdd + "&phone=" + strPhone + "&content=" + strContent;
        String strRes = SmsHttpSend.postSend(strSmsUrl, strSmsParam);
        String nowTime = DateTimeUtils.getLocalDateTime();
        validateNum = validateNum + nowTime;
        smsCacheMap.put(phoneNum, validateNum);
        return strRes;
    }

    public static String checkBalance() {
        String strReg = sms_account;
        String strPwd = sms_password;
        String strBalanceUrl = "http://www.stongnet.com/sdkhttp/getbalance.aspx";
        String strBalanceParam = "reg=" + strReg + "&pwd=" + strPwd;
        String strRes = SmsHttpSend.postSend(strBalanceUrl, strBalanceParam);
        return strRes;
    }

    public static Map<String, String> getValidateNum() {
        return smsCacheMap;
    }

    public static void removePhoneNum(String phoneNum) {
        smsCacheMap.remove(phoneNum);
    }

    private static String createValidateNum() {
        Random r = new Random();
        int number = r.nextInt(9000) + 1000;
        return number + "";
    }


}
